﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.DB;
using YT.Commons;
using YT.Modules;


namespace YT.XH.NOSE
{
    public partial class frmNSLabExamDetail : Form
    {

        private string mstrSeqNo = string.Empty;
        private string mSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private FormStatus mfrmStatus = new FormStatus();
        private bool mblnExit = false;
        private string mstrTableName = "NoseLabExamDtl";

        public frmNSLabExamDetail()
        {
            InitializeComponent();
        }

        public frmNSLabExamDetail(string strSeqNo,string strSickRecID)
        {
            InitializeComponent();
            mstrSeqNo = strSeqNo ;
            mSickRecID = strSickRecID;
        }

        private bool UpdateDetail()
        {
            if (!ValidCheck()) return false ;
            this.dgvDetail.EndEdit();
            string strSQL = string.Empty;
            DataRow drRow = null;
            if (mstrSeqNo == "0" || mstrSeqNo == "")
            {
                drRow = mdbcon.GetDataTable("select top 0 * from NoseLabExamHdr").NewRow();
                mstrSeqNo = (CvtFunc.ToInt(DataFunctions.GetFieldValue("NoseLabExamHdr", "max(SeqNo) as SeqNo", " 1=1 and SickRecID='"+ mSickRecID +"' ")) + 1).ToString();
                strSQL = "insert into NoseLabExamHdr ( SickRecID,SeqNo,LabExamSeqNo,LabExamDate,NextExamDate  )";
                strSQL += " Values ( '{0}','{1}','{2}','{3}','{4}' ) ";
                strSQL = string.Format(strSQL, mSickRecID, mstrSeqNo ,txtSickID.Text, (BaseFunctions.IsDate(datExamDate.Value) ? datExamDate.Value : null), (BaseFunctions.IsDate(datNextDate.Value) ? datExamDate.Value : null));
                mdbcon.ExecuteSql(strSQL);
            }
            else
            {
                //drRow =mdbcon.GetDataFirstRow( "select top 1 * from NoseLabExamHdr where SeqNo='"+ mstrSeqNo +"'");
                strSQL = "update NoseLabExamHdr set NextExamDate='{0}', LabExamSeqNo='{1}' where seqno='"+ mstrSeqNo +"' and SickRecID='"+ mSickRecID +"'";
                strSQL = string.Format(strSQL, (BaseFunctions.IsDate(datNextDate.Value) ? datExamDate.Value : null), txtSickID.Text);
                mdbcon.ExecuteSql(strSQL); 
            }

            strSQL = "Delete from "+ mstrTableName +" where hdrseqno='"+ mstrSeqNo +"' and SickRecID='"+ mSickRecID +"'";
            mdbcon.ExecuteSql(strSQL);

            foreach (DataGridViewRow dgvRow in this.dgvDetail.Rows)
            {
                string strCode = CvtFunc.ToString (dgvRow.Cells[ColCode.Index].Value);
                string strCodeDesc =CvtFunc.ToString ( dgvRow.Cells[ColDesc.Index].Value);
                string strLevel =CvtFunc.ToString ( dgvRow.Cells[ColLevel.Index].Value);
                strSQL = " INSERT INTO " + mstrTableName + "  (HdrSeqNo,SickRecID,Code,CodeDesc,Result) " +
                         " VALUES ( '{0}','{1}','{2}','{3}','{4}') ";
                strSQL = string.Format(strSQL, mstrSeqNo, mSickRecID, strCode, strCodeDesc,CvtFunc.ToDouble(strLevel));
                mdbcon.ExecuteSql(strSQL);
            }
            return true;
        }

        private void FillGrid()
        {
            this.dgvDetail.Rows.Clear();
            string strSQL="select * from "+ mstrTableName +" where HdrSeqNo='"+ mstrSeqNo +"' and SickRecID='"+ mSickRecID +"'";
            DataTable dtDetail = mdbcon.GetDataTable(strSQL);
            if (dtDetail.Rows.Count != 0)
            {
                foreach (DataRow drRow in dtDetail.Rows)
                {
                    this.dgvDetail.Rows.Add(); 
                    this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColCode.Index].Value = CvtFunc.ToString ( drRow["Code"])  ;
                    this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColDesc.Index].Value = CvtFunc.ToString(drRow["CodeDesc"]);
                    this.dgvDetail.Rows[this.dgvDetail.Rows.Count - 1].Cells[ColLevel.Index].Value =BaseFunctions.FormatNumeric(  CvtFunc.ToString( drRow["Result"]) ,"Ns.1")  ;  
                }
            }
            else
            {
                for (int intRow = 0; intRow <= 6; intRow++)
                {
                    string strTmpCode = string.Empty;
                    string strTmpCodeDesc = string.Empty;
                    if (intRow == 0)
                    {
                        strTmpCode = "Dx";
                        strTmpCodeDesc = "户尘螨粉尘螨"; 
                    }
                    if (intRow == 1)
                    {
                        strTmpCode = "Wx";
                        strTmpCodeDesc = "矮豚草蒿葏草"; 
                    }
                    if (intRow == 2)
                    {
                        strTmpCode = "E1/E5";
                        strTmpCodeDesc = "猫毛皮屑狗毛皮屑"; 
                    }
                    if (intRow == 3)
                    {
                        strTmpCode = "I6";
                        strTmpCodeDesc = "蟑螂"; 
                    }
                    if (intRow == 4)
                    {
                        strTmpCode = "Mx1";
                        strTmpCodeDesc = "点青霉分枝孢霉烦曲霉交链孢霉"; 
                    }
                    if (intRow == 5)
                    {
                        strTmpCode = "Tx4";
                        strTmpCodeDesc = "柏榆柳桐杨三角叶杨"; 
                    }
                    if (intRow == 6)
                    {
                        strTmpCode = "IgE";
                        strTmpCodeDesc = "IgE"; 
                    }
                    
                    this.dgvDetail.Rows.Add();
                    this.dgvDetail.Rows[intRow].Cells[ColCode.Index].Value =  strTmpCode  ;
                    this.dgvDetail.Rows[intRow].Cells[ColDesc.Index].Value = strTmpCodeDesc  ;
                    this.dgvDetail.Rows[intRow].Cells[ColLevel.Index].Value = "0.0";                    

                }
                this.dgvDetail.Rows.Add();
            }
            
            this.dgvDetail.Rows[7].Cells[ColCode.Index].Value = "Kontrolle";
            this.dgvDetail.Rows[7].Cells[ColDesc.Index].Value = "16.2";
            this.dgvDetail.Rows[7].Cells[ColLevel.Index].Value = "";
            this.dgvDetail.Rows[7].Height = 150;
            this.dgvDetail.Rows[7].DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft;

            this.dgvDetail.EndEdit(); 
        }

        private bool ValidCheck()
        {
            if (txtSickID.Text == "")
            {
                MessageBox.Show("[患者编号] 不能为空！", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (UpdateDetail()) this.Close(); 
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (!UpdateDetail()) return;
            }
            this.Close();
        }

        private void FillBaseInfo()
        {
            txtSickName.Text =  DataFunctions.GetFieldValue("mSickPerson","SickName","SickRecID='"+ mSickRecID +"'");
            if (mstrSeqNo == "" || mstrSeqNo == "0")
            {
                datExamDate.Value = DateTime.Now.Date.ToString() ;  
            }
            else 
            {
                string strSQL= "select * from NoseLabExamHdr where SeqNo='"+ mstrSeqNo +"' and SickRecID='"+ mSickRecID +"' ";
                DataRow drRow = mdbcon.GetDataFirstRow ( strSQL );
                if ( drRow!=null )
                {
                    datExamDate.Value = BaseFunctions.FormatDate( CvtFunc.ToString ( drRow["LabExamDate"] ), DateFormatOption.DateFmtYMD);
                    datNextDate.Value = BaseFunctions.FormatDate(CvtFunc.ToString(drRow["NextExamDate"] ), DateFormatOption.DateFmtYMD);
                    txtSickID.Text = CvtFunc.ToString(drRow["LabExamSeqNo"]);
                }                
            }
        }

        private void frmNSLabExamDetail_Load(object sender, EventArgs e)
        {
            FillBaseInfo();
            FillGrid();
            this.Text = "实验室检查";
            timer1.Start(); 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
            {
                timer1.Stop();
            }
            this.Opacity += 0.04;
        }
    }
}
